//
//  IHTestVM.swift
//  InternetHospital
//
//  Created by Shelby on 2019/1/11.
//  Copyright © 2019 ZhuJiangChilink. All rights reserved.
//

import Foundation
import JKNetwork
import RxSwift
class IHTestVM: NSObject {
    fileprivate var obsv = PublishSubject<Root>()
    var obs: PublishSubject<Root> {
        return obsv.asObservable() as! PublishSubject<Root>
    }
    override init() {
        
        super.init()
    }
    
//var obj: Variable<Root>?
    func getData(completion: ((_ error: NSError?, _ root: Root?) -> Void)?) -> Void {
        TestAPI.weatherApi {
            (err, js: Any?) in
            if err != nil || js == nil {
                completion?(NSError(domain: "服务器错误", code: -1, userInfo: nil),nil)
                return
            }
            let jsonDecoder = JSONDecoder()
            let jsonData = try? JSONSerialization.data(withJSONObject: js!, options: [])
            guard let jd = jsonData else {
                completion?(NSError(domain: "服务器错误", code: -1, userInfo: nil),nil)
                return
            }
            let model: Root = try! jsonDecoder.decode(Root.self, from: jd)
//            self.obj?.value = model
            self.obsv.onNext(model)
            completion?(nil,model)
            return
        }.resume()
    }
    
    
}

class TestAPI: NSObject {

  class  func weatherApi(completion: @escaping JKNetworkCompletionHandler) ->  IHNetworking {
        let url = "https://yjyf.qdting.com/api/faq/"
    
        return IHNetworking.get(withUrl: url, parameters: nil, completionHandler: completion)
        }
    }

